


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > PacManUiBuilder</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.ui</a>
</div>

<h1>Coverage Summary for Class: PacManUiBuilder (nl.tudelft.jpacman.ui)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">PacManUiBuilder</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/9)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/30)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.ui;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import java.util.HashMap;
<i class="no-highlight">4</i>&nbsp;import java.util.LinkedHashMap;
<i class="no-highlight">5</i>&nbsp;import java.util.Map;
<i class="no-highlight">6</i>&nbsp;
<i class="no-highlight">7</i>&nbsp;import nl.tudelft.jpacman.game.Game;
<i class="no-highlight">8</i>&nbsp;import nl.tudelft.jpacman.ui.ScorePanel.ScoreFormatter;
<i class="no-highlight">9</i>&nbsp;
<i class="no-highlight">10</i>&nbsp;/**
<i class="no-highlight">11</i>&nbsp; * Builder for the JPac-Man UI.
<i class="no-highlight">12</i>&nbsp; *
<i class="no-highlight">13</i>&nbsp; * @author Jeroen Roosen 
<i class="no-highlight">14</i>&nbsp; */
<b class="nc"><i class="no-highlight">15</i>&nbsp;public class PacManUiBuilder {</b>
<i class="no-highlight">16</i>&nbsp;
<i class="no-highlight">17</i>&nbsp;    /**
<i class="no-highlight">18</i>&nbsp;     * Caption for the default stop button.
<i class="no-highlight">19</i>&nbsp;     */
<i class="no-highlight">20</i>&nbsp;    private static final String STOP_CAPTION = &quot;Stop&quot;;
<i class="no-highlight">21</i>&nbsp;
<i class="no-highlight">22</i>&nbsp;    /**
<i class="no-highlight">23</i>&nbsp;     * Caption for the default start button.
<i class="no-highlight">24</i>&nbsp;     */
<i class="no-highlight">25</i>&nbsp;    private static final String START_CAPTION = &quot;Start&quot;;
<i class="no-highlight">26</i>&nbsp;
<i class="no-highlight">27</i>&nbsp;    /**
<i class="no-highlight">28</i>&nbsp;     * Map of buttons and their actions.
<i class="no-highlight">29</i>&nbsp;     */
<i class="no-highlight">30</i>&nbsp;    private final Map&lt;String, Action&gt; buttons;
<i class="no-highlight">31</i>&nbsp;
<i class="no-highlight">32</i>&nbsp;    /**
<i class="no-highlight">33</i>&nbsp;     * Map of key events and their actions.
<i class="no-highlight">34</i>&nbsp;     */
<i class="no-highlight">35</i>&nbsp;    private final Map&lt;Integer, Action&gt; keyMappings;
<i class="no-highlight">36</i>&nbsp;
<i class="no-highlight">37</i>&nbsp;    /**
<i class="no-highlight">38</i>&nbsp;     * &lt;code&gt;true&lt;/code&gt; iff this UI has the default buttons.
<i class="no-highlight">39</i>&nbsp;     */
<i class="no-highlight">40</i>&nbsp;    private boolean defaultButtons;
<i class="no-highlight">41</i>&nbsp;
<i class="no-highlight">42</i>&nbsp;    /**
<i class="no-highlight">43</i>&nbsp;     * Way to format the score.
<i class="no-highlight">44</i>&nbsp;     */
<b class="nc"><i class="no-highlight">45</i>&nbsp;    private ScoreFormatter scoreFormatter = null;</b>
<i class="no-highlight">46</i>&nbsp;
<i class="no-highlight">47</i>&nbsp;    /**
<i class="no-highlight">48</i>&nbsp;     * Creates a new Pac-Man UI builder without any mapped keys or buttons.
<i class="no-highlight">49</i>&nbsp;     */
<b class="nc"><i class="no-highlight">50</i>&nbsp;    public PacManUiBuilder() {</b>
<b class="nc"><i class="no-highlight">51</i>&nbsp;        this.defaultButtons = false;</b>
<b class="nc"><i class="no-highlight">52</i>&nbsp;        this.buttons = new LinkedHashMap&lt;&gt;();</b>
<b class="nc"><i class="no-highlight">53</i>&nbsp;        this.keyMappings = new HashMap&lt;&gt;();</b>
<i class="no-highlight">54</i>&nbsp;    }
<i class="no-highlight">55</i>&nbsp;
<i class="no-highlight">56</i>&nbsp;    /**
<i class="no-highlight">57</i>&nbsp;     * Creates a new Pac-Man UI with the set keys and buttons.
<i class="no-highlight">58</i>&nbsp;     *
<i class="no-highlight">59</i>&nbsp;     * @param game
<i class="no-highlight">60</i>&nbsp;     *            The game to build the UI for.
<i class="no-highlight">61</i>&nbsp;     * @return A new Pac-Man UI with the set keys and buttons.
<i class="no-highlight">62</i>&nbsp;     */
<i class="no-highlight">63</i>&nbsp;    public PacManUI build(final Game game) {
<b class="nc"><i class="no-highlight">64</i>&nbsp;        assert game != null;</b>
<i class="no-highlight">65</i>&nbsp;
<b class="nc"><i class="no-highlight">66</i>&nbsp;        if (defaultButtons) {</b>
<b class="nc"><i class="no-highlight">67</i>&nbsp;            addStartButton(game);</b>
<b class="nc"><i class="no-highlight">68</i>&nbsp;            addStopButton(game);</b>
<i class="no-highlight">69</i>&nbsp;        }
<b class="nc"><i class="no-highlight">70</i>&nbsp;        return new PacManUI(game, buttons, keyMappings, scoreFormatter);</b>
<i class="no-highlight">71</i>&nbsp;    }
<i class="no-highlight">72</i>&nbsp;
<i class="no-highlight">73</i>&nbsp;    /**
<i class="no-highlight">74</i>&nbsp;     * Adds a button with the caption {@value #STOP_CAPTION} that stops the
<i class="no-highlight">75</i>&nbsp;     * game.
<i class="no-highlight">76</i>&nbsp;     *
<i class="no-highlight">77</i>&nbsp;     * @param game
<i class="no-highlight">78</i>&nbsp;     *            The game to stop.
<i class="no-highlight">79</i>&nbsp;     */
<i class="no-highlight">80</i>&nbsp;    private void addStopButton(final Game game) {
<b class="nc"><i class="no-highlight">81</i>&nbsp;        assert game != null;</b>
<i class="no-highlight">82</i>&nbsp;
<b class="nc"><i class="no-highlight">83</i>&nbsp;        buttons.put(STOP_CAPTION, game::stop);</b>
<i class="no-highlight">84</i>&nbsp;    }
<i class="no-highlight">85</i>&nbsp;
<i class="no-highlight">86</i>&nbsp;    /**
<i class="no-highlight">87</i>&nbsp;     * Adds a button with the caption {@value #START_CAPTION} that starts the
<i class="no-highlight">88</i>&nbsp;     * game.
<i class="no-highlight">89</i>&nbsp;     *
<i class="no-highlight">90</i>&nbsp;     * @param game
<i class="no-highlight">91</i>&nbsp;     *            The game to start.
<i class="no-highlight">92</i>&nbsp;     */
<i class="no-highlight">93</i>&nbsp;    private void addStartButton(final Game game) {
<b class="nc"><i class="no-highlight">94</i>&nbsp;        assert game != null;</b>
<i class="no-highlight">95</i>&nbsp;
<b class="nc"><i class="no-highlight">96</i>&nbsp;        buttons.put(START_CAPTION, game::start);</b>
<i class="no-highlight">97</i>&nbsp;    }
<i class="no-highlight">98</i>&nbsp;
<i class="no-highlight">99</i>&nbsp;    /**
<i class="no-highlight">100</i>&nbsp;     * Adds a key listener to the UI.
<i class="no-highlight">101</i>&nbsp;     *
<i class="no-highlight">102</i>&nbsp;     * @param keyCode
<i class="no-highlight">103</i>&nbsp;     *            The key code of the key as used by {@link java.awt.event.KeyEvent}.
<i class="no-highlight">104</i>&nbsp;     * @param action
<i class="no-highlight">105</i>&nbsp;     *            The action to perform when the key is pressed.
<i class="no-highlight">106</i>&nbsp;     * @return The builder.
<i class="no-highlight">107</i>&nbsp;     */
<i class="no-highlight">108</i>&nbsp;    public PacManUiBuilder addKey(Integer keyCode, Action action) {
<b class="nc"><i class="no-highlight">109</i>&nbsp;        assert keyCode != null;</b>
<b class="nc"><i class="no-highlight">110</i>&nbsp;        assert action != null;</b>
<i class="no-highlight">111</i>&nbsp;
<b class="nc"><i class="no-highlight">112</i>&nbsp;        keyMappings.put(keyCode, action);</b>
<b class="nc"><i class="no-highlight">113</i>&nbsp;        return this;</b>
<i class="no-highlight">114</i>&nbsp;    }
<i class="no-highlight">115</i>&nbsp;
<i class="no-highlight">116</i>&nbsp;    /**
<i class="no-highlight">117</i>&nbsp;     * Adds a button to the UI.
<i class="no-highlight">118</i>&nbsp;     *
<i class="no-highlight">119</i>&nbsp;     * @param caption
<i class="no-highlight">120</i>&nbsp;     *            The caption of the button.
<i class="no-highlight">121</i>&nbsp;     * @param action
<i class="no-highlight">122</i>&nbsp;     *            The action to execute when the button is clicked.
<i class="no-highlight">123</i>&nbsp;     * @return The builder.
<i class="no-highlight">124</i>&nbsp;     */
<i class="no-highlight">125</i>&nbsp;    public PacManUiBuilder addButton(String caption, Action action) {
<b class="nc"><i class="no-highlight">126</i>&nbsp;        assert caption != null;</b>
<b class="nc"><i class="no-highlight">127</i>&nbsp;        assert !caption.isEmpty();</b>
<b class="nc"><i class="no-highlight">128</i>&nbsp;        assert action != null;</b>
<i class="no-highlight">129</i>&nbsp;
<b class="nc"><i class="no-highlight">130</i>&nbsp;        buttons.put(caption, action);</b>
<b class="nc"><i class="no-highlight">131</i>&nbsp;        return this;</b>
<i class="no-highlight">132</i>&nbsp;    }
<i class="no-highlight">133</i>&nbsp;
<i class="no-highlight">134</i>&nbsp;    /**
<i class="no-highlight">135</i>&nbsp;     * Adds a start and stop button to the UI. The actual actions for these
<i class="no-highlight">136</i>&nbsp;     * buttons will be added upon building the UI.
<i class="no-highlight">137</i>&nbsp;     *
<i class="no-highlight">138</i>&nbsp;     * @return The builder.
<i class="no-highlight">139</i>&nbsp;     */
<i class="no-highlight">140</i>&nbsp;    public PacManUiBuilder withDefaultButtons() {
<b class="nc"><i class="no-highlight">141</i>&nbsp;        defaultButtons = true;</b>
<b class="nc"><i class="no-highlight">142</i>&nbsp;        buttons.put(START_CAPTION, null);</b>
<b class="nc"><i class="no-highlight">143</i>&nbsp;        buttons.put(STOP_CAPTION, null);</b>
<b class="nc"><i class="no-highlight">144</i>&nbsp;        return this;</b>
<i class="no-highlight">145</i>&nbsp;    }
<i class="no-highlight">146</i>&nbsp;
<i class="no-highlight">147</i>&nbsp;    /**
<i class="no-highlight">148</i>&nbsp;     * Provide formatter for the score.
<i class="no-highlight">149</i>&nbsp;     *
<i class="no-highlight">150</i>&nbsp;     * @param scoreFormatter
<i class="no-highlight">151</i>&nbsp;     *         The score formatter to be used.
<i class="no-highlight">152</i>&nbsp;     *
<i class="no-highlight">153</i>&nbsp;     * @return The builder.
<i class="no-highlight">154</i>&nbsp;     */
<i class="no-highlight">155</i>&nbsp;    public PacManUiBuilder withScoreFormatter(ScoreFormatter scoreFormatter) {
<b class="nc"><i class="no-highlight">156</i>&nbsp;        this.scoreFormatter = scoreFormatter;</b>
<b class="nc"><i class="no-highlight">157</i>&nbsp;        return this;</b>
<i class="no-highlight">158</i>&nbsp;    }
<i class="no-highlight">159</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
